package com.addcn.caruimodule.viewpager;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.addcn.caruimodule.R;

/**
 * Created by 10464 on 2016/11/3.
 */

public class CustomViewPager extends ViewPager {
    private float mDownX=0;
    private float mDownY=0;
    private View parentView;
    private boolean isScroll = true;
    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomViewPager);
        isScroll = typedArray.getBoolean(R.styleable.CustomViewPager_is_scroll, isScroll);
    }

    public void setParentView(View parentView) {
        this.parentView = parentView;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (parentView != null){
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //記錄下按下時當前的xy左標
                    mDownX = ev.getX();
                    mDownY = ev.getY();
                    getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_MOVE:
//                //滑動時計算x軸y軸的移動距離 ，如果x軸移動距離大於y軸，說明是水平滑動，禁止下拉重新整理
                    if (Math.abs(ev.getX() - mDownX) > Math.abs(ev.getY() - mDownY)) {
                        getParent().requestDisallowInterceptTouchEvent(true);
//                        if (parentView instanceof PullableListView) {
//                            ((PullableListView) parentView).setCanPullDown(false);
//                        }
                    } else {
                        getParent().requestDisallowInterceptTouchEvent(false);
//                        if (parentView instanceof PullableListView) {
//                            ((PullableListView) parentView).setCanPullDown(true);
//                        }
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    getParent().requestDisallowInterceptTouchEvent(false);
//                    if (parentView instanceof PullableListView) {
//                        ((PullableListView) parentView).setCanPullDown(true);
//                    }
                    break;
                default:
                    break;
            }
        }
        return super.dispatchTouchEvent(ev);
    }


    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (isScroll){
            try {
                return super.onTouchEvent(arg0);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }
            return false;
        } else
            return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (isScroll){
            try {
                return super.onInterceptTouchEvent(arg0);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }
            return false;
        } else
            return false;

    }

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if (v != this && v instanceof ViewPager) {
            return true;
        }
        return super.canScroll(v, checkV, dx, x, y);
    }
}
